package me.samlss.lighter_demo;

import me.samlss.lighter.util.PixelUtil;
import me.samlss.lighter.util.Preconditions;
import me.samlss.lighter_demo.tools.Toast;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import org.junit.Assert;
import org.junit.Test;

public class ExampleOhosTest {
    @Test
    public void testScreenHeight() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();

        PixelUtil.initContext(context);
        int height = PixelUtil.screenHeight();
        System.out.println("height: " + height);
        Assert.assertTrue(height > 0);
        Assert.assertEquals(height, 2085);
    }

    @Test
    public void testScreenWidth() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();

        PixelUtil.initContext(context);
        int width = PixelUtil.screenWidth();
        Assert.assertTrue(width > 0);
        Assert.assertEquals(width, 1080);
    }

    @Test
    public void testScreenVp2px() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();

        PixelUtil.initContext(context);
        int width = (int) PixelUtil.vp2px(1);
        Assert.assertTrue(width == 3);
    }

    @Test
    public void testPreconditionsCheckNotNull() {
        Object obj = null;
        try {
            Preconditions.checkNotNull(obj, "obj must not be null");
        }catch (Exception e){
            System.out.println(e.getMessage());
//            e.printStackTrace();
        }
        obj = new Object();
        Object objMustNotBeNull = Preconditions.checkNotNull(obj, "obj must not be null");
        Assert.assertNotNull(objMustNotBeNull);
    }


    @Test
    public void testPreconditionsCheckNotEmpty() {
        String string = null;
        try {
            Preconditions.checkNotEmpty(string);
        }catch (Exception e){
            Assert.assertTrue("Must not be null or empty".equals(e.getMessage()));
//            e.printStackTrace();
        }
        string = "not empty";
        String StringMustNotBeNull = Preconditions.checkNotEmpty(string);
        Assert.assertNotNull(StringMustNotBeNull);
    }

    @Test
    public void testToastShow() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
        handler.postTask(() -> {
            Toast.show(context, "Click to show highlight");
        });
    }
}